{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# AGiXT Voice Chat\n",
    "\n",
    "Use the AGiXTListen class to make your agent listen for you to say a specific word that makes it take what you say and send it to the agent.\n",
    "\n",
    "In the example below, we will use use two different wake functions, `chat` and `instruct`.\n",
    "\n",
    "- If you say the word `chat` while it is running, it will take anything you say after the word `chat` and send it to the agent, then the agent will send an audio response back to you.\n",
    "- If you say the word `instruct` while it is running, it will take anything you say after the word `instruct` and send it to the agent, which will be enabled to execute any commands the agent has available to it, then respond back to you with an audio response.\n",
    "\n",
    "Both of these functions are built in and are default if you do not define any wake functions. We are only showing them here to demonstrate how to build your own wake functions.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from Listen import AGiXTListen\n",
    "from agixtsdk import AGiXTSDK\n",
    "from datetime import datetime\n",
    "\n",
    "AGIXT_SERVER = \"http://localhost:7437\"\n",
    "API_KEY = \"\"\n",
    "AGENT_NAME = \"gpt4free\"\n",
    "\n",
    "sdk = AGiXTSDK(\n",
    "    base_uri=AGIXT_SERVER,\n",
    "    api_key=API_KEY,\n",
    ")\n",
    "\n",
    "\n",
    "# Wake function take one input only, the transcribed text.\n",
    "def voice_chat(text):\n",
    "    print(f\"Sending text to agent: {text}\")\n",
    "    text_response = sdk.chat(\n",
    "        agent_name=AGENT_NAME,\n",
    "        user_input=text,\n",
    "        conversation=datetime.now().strftime(\"%Y-%m-%d\"),\n",
    "        context_results=4,\n",
    "    )\n",
    "    return text_response\n",
    "\n",
    "\n",
    "def voice_instruct(text):\n",
    "    print(f\"Sending text to agent: {text}\")\n",
    "    text_response = sdk.instruct(\n",
    "        agent_name=AGENT_NAME,\n",
    "        user_input=text,\n",
    "        conversation=datetime.now().strftime(\"%Y-%m-%d\"),\n",
    "    )\n",
    "    return text_response\n",
    "\n",
    "\n",
    "# Wake functions are stored in a dictionary, the key is the wake word and the value is the function.\n",
    "WAKE_FUNCTIONS = {\n",
    "    \"chat\": voice_chat,\n",
    "    \"instruct\": voice_instruct,\n",
    "}\n",
    "\n",
    "listener = AGiXTListen(\n",
    "    server=AGIXT_SERVER,\n",
    "    api_key=API_KEY,\n",
    "    agent_name=AGENT_NAME,\n",
    "    wake_functions=WAKE_FUNCTIONS,\n",
    "    wake_word=\"hey assistant\",  # Make sure this matches your intended wake word\n",
    ")\n",
    "listener.listen()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Command line option\n",
    "\n",
    "Run from command line with default wake functions (`chat` and `instruct`). Set the `server`, `agent_name`, and `api_key` if needed.\n",
    "\n",
    "- `--server` - The server to connect to. Default: `http://localhost:7437`\n",
    "- `--agent_name` - The name of the agent to connect to. Default: `gpt4free`\n",
    "- `--api_key` (Optional) - The API key to use.\n",
    "\n",
    "```bash\n",
    "git clone https://github.com/Josh-XT/AGiXT\n",
    "cd AGiXT/agixt/examples\n",
    "python3 Listen.py --server http://localhost:7437 --agent_name gpt4free\n",
    "```\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
